package com.tyqx.hashchainbiz;

// import com.alibaba.fastjson.JSON;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.tyqx.hashchainbean.module.JZTGS1Attachment;
import com.tyqx.hashchainbean.module.NoHashValueJson;
import com.tyqx.hashchainbiz.controller.mongodb.MongoDBGridFSUtil;
import com.tyqx.hashchainbiz.service.NoHashValueJsonService;
import hashchainutil.common.EmptyUtils;
import hashchainutil.common.JsonUtil;
import org.bson.types.ObjectId;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;

@Component
public class SchedulerMongoDBGridFSGS1 {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Resource
    private NoHashValueJsonService noHashValueJsonService;


    @Resource
    private MongoDatabase mongoDatabase;

    ///@Async
    //表示每隔秒
//    @Scheduled(fixedRate = 60 * 1000 * 10)
//    @Transactional(rollbackFor = Exception.class)
    @Scheduled(fixedDelay = 1000 * 12)
    public void testTasks() throws Exception {
        System.out.println("GS1读取附件定时任务执行时间：" + dateFormat.format(new Date()));
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        String collNameJtb = "jztGS1AttachmentList";
        GridFSBucket gridFSFilesBucket = GridFSBuckets.create(mongoDatabase,collNameJtb);
        List<JZTGS1Attachment> jztgs1AttachmentList=MongoDBGridFSUtil.getJZTGS1AttachmentFile(gridFSFilesBucket);
        if (EmptyUtils.isNotEmpty(jztgs1AttachmentList)) {
            int size = jztgs1AttachmentList.size();
            CryptoPrimitives suite = new CryptoPrimitives();
            suite.init();
            List<NoHashValueJson> noHashValueJsonList=new ArrayList<>();
            List<ObjectId> objectIds=new ArrayList<>();
            for (int i = 0; i < size; i++) {
                JZTGS1Attachment jztgs1Attachment = jztgs1AttachmentList.get(i);
                Date currentDate = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date createTime = sdf.parse(sdf.format(currentDate));
                jztgs1Attachment.setCreateTime(createTime);
                jztgs1Attachment.setUpdateTime(createTime);

                JSONObject jsonObject = new JSONObject(gson.toJson(jztgs1Attachment));
                jsonObject = JsonUtil.getSortJson(jsonObject);
                String jztAttachmentBusinessStr = jsonObject.toString();

                NoHashValueJson noHashValueJson = new NoHashValueJson();
                noHashValueJson.setRecordJson(jztAttachmentBusinessStr);
                noHashValueJson.setHashTableName("jzt_GS1_attachment");
                noHashValueJson.setUserName(jztgs1Attachment.getUserName());
                byte[] hashBytes = suite.hash(jztAttachmentBusinessStr.getBytes());
                String hashValue = Base64.getEncoder().encodeToString(hashBytes);

                noHashValueJson.setHashValue(hashValue);

                noHashValueJson.setStatus(0);

                noHashValueJsonList.add(noHashValueJson);
                ObjectId objectId = new ObjectId(jztgs1Attachment.getObjectId());
                objectIds.add(objectId);
            }
            Integer flag = noHashValueJsonService.insertNoHashValueJsonBatch(noHashValueJsonList);
            if (flag > 0) {
                String fileName="mongodb-jzt-GS1-attachment-1";
                noHashValueJsonService.insertMongoDBBatch(objectIds, gridFSFilesBucket,fileName);
            }
        }

        System.out.println("GS1读取附件定时任务结束时间：" + dateFormat.format(new Date()));
    }

}